package de.deliancourt.jarvis.main;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.*;
import com.sun.j3d.utils.behaviors.mouse.MouseBehavior;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.behaviors.vp.*;

import de.deliancourt.jarvis.implementations.BaseWindow;
import de.deliancourt.jarvis.implementations.Configuration;
import de.deliancourt.jarvis.implementations.FpsBehavior;
import de.deliancourt.jarvis.implementations.Tile;
import de.deliancourt.jarvis.implementations.WindowSide;
import de.deliancourt.jarvis.media.textures.*;

import javax.swing.JFrame;


public class Test extends JFrame {

	/**
	 * @param args
	 */
	public Canvas3D myCanvas3D;
	
	public Test()
	{
		//Mechanism for closing the window and ending the program.
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	   
	    Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
	    add("Center", canvas);
	    SimpleUniverse u = new SimpleUniverse(canvas);
	    //canvas.getView().setSceneAntialiasingEnable(true);

	   
	    ViewingPlatform vp = u.getViewingPlatform();
	    
	    vp.getViewers()[0].getView().setFrontClipDistance(3);
	    vp.getViewers()[0].getView().setBackClipDistance(30000000.0);
	    TransformGroup vptg =vp.getMultiTransformGroup().getTransformGroup(0);
	    Transform3D T3d = new Transform3D();
	    
	    
	    vptg.getTransform(T3d);
	    T3d.setTranslation(new Vector3d(-0,0,60));
	    vptg.setTransform(T3d);	   
	    
	    //u.getViewingPlatform().setNominalViewingTransform();  // back away from object a little
	   
	    BranchGroup objRoot = new BranchGroup();
	    
	    
	    final BaseWindow bw = new BaseWindow();
	    
	    
	    //WindowSide bw = new WindowSide(new Point3d(0,0,0 ),new Point2d(10,10), WindowSide.XZ,1,3); 
	    
	    //Tile bw = new Tile(new Point3d(0,0,0),10,10,Tile.XZ);
	    
	    TransformGroup objRotate = new TransformGroup();
	    TransformGroup winRotate = new TransformGroup();
	    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	    
	    
	   // objRoot.addChild(bw);
	    winRotate.addChild(bw);
	    
	    //The bounding region for the background.
	    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),Double.MAX_VALUE);
	    //Load the background image.
	    Configuration conf = null;
	    Background bgImage = new Background();
		try {
			conf = new Configuration();
			
			 bgImage= conf.GetBackground();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   
	    //Define the image as the background and add it to the scene.
		
		
		Transform3D Tcube = new Transform3D();
		TransformGroup tgCube = new TransformGroup();
		Vector3d v = new Vector3d();
		Vector3d v2 = new Vector3d();
		T3d.get(v);
		v2=(Vector3d) v.clone();
		
		v2.z=30;
		v2.x=-10;
		v2.y=-7;
		System.out.println("X: " + v.x +";Y: " + v.y +";Z: " + v.z);
		System.out.println("X: " + v2.x +";Y: " + v2.y +";Z: " + v2.z);
		Tcube.setTranslation(v2);
		//
		
		
	    bgImage.setApplicationBounds(bounds);
	    objRoot.addChild(bgImage);
	    
	   
	    MouseRotate myMouseRotate = new MouseRotate();
	    MouseWheelZoom mz = new MouseWheelZoom();
	    mz.setFactor(1);
	   mz.setTransformGroup(winRotate);
	    mz.setSchedulingBounds(bounds);
	    TransformGroup tgDreher = new TransformGroup();
	    
	     objRotate.setTransform(Tcube); 
	    Alpha spinAlpha = new Alpha(-1, 100000);
	    RotationInterpolator dreher = new RotationInterpolator(spinAlpha, tgDreher);
	    dreher.setMaximumAngle(-100.0f);
	    BoundingSphere zone = new BoundingSphere();
	    dreher.setSchedulingBounds(zone);
	     
	     
	    
	     //Tcube.mul(dreher.getTransformAxis());
	   
	     
	    tgDreher.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	    tgDreher.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	    winRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	    winRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	    //tgDreher.addChild(dreher);
	    Tcube.mul(dreher.getTransformAxis());
	    
	    Text2D fps = new Text2D("FPS: 100.0", new Color3f(0.6f, 0.85f, 0.9f), "Arial", 16, Font.BOLD);
	    Text2D demo = new Text2D("DeLiancourt Design - Demo", new Color3f(0.6f, 0.85f, 0.9f), "Arial", 16, Font.BOLD);
	    fps.getAppearance().setCapability (Appearance.ALLOW_TEXTURE_READ);
	    fps.getAppearance().setCapability (Appearance.ALLOW_TEXTURE_WRITE);
	    FpsBehavior f = new FpsBehavior(fps);  
	    TransformGroup tgtext = new TransformGroup();
	    Transform3D t3dtext = new Transform3D();
	    TransformGroup tgtext2 = new TransformGroup();
	    Transform3D t3dtext2 = new Transform3D();
	    
	    f.setSchedulingBounds(new BoundingSphere());
	    t3dtext.setTranslation(new Vector3d(-24,19,0));
	    t3dtext.setScale(10);
	    t3dtext2.setTranslation(new Vector3d(-24,-18,0));
	    t3dtext2.setScale(10);
	    tgtext.setTransform(t3dtext);
	    tgtext.addChild(fps);
	    tgtext2.setTransform(t3dtext2);
	    tgtext2.addChild(demo);
	     tgDreher.addChild(dreher);
	     tgDreher.addChild(conf.GetLogo());
	     objRotate.addChild(tgDreher);
	     objRoot.addChild(objRotate);
	     
	     objRoot.addChild(f);
	     
	     objRoot.addChild(tgtext);
	     objRoot.addChild(tgtext2);
	    myMouseRotate.setTransformGroup(winRotate);
	    myMouseRotate.setSchedulingBounds(new BoundingSphere());
	    myMouseRotate.setFactor(0.01, 0.01);
       objRoot.addChild(winRotate);
       objRoot.addChild(myMouseRotate);
	    
	    addLight(u);
	    objRoot.compile();
	    u.addBranchGraph(objRoot);

	    //Show the canvas/window.
	    setTitle("An image as a background");
	    setSize(1200,1000);
	    getContentPane().add("Center",canvas);
	    setVisible(true);
	    
	}
	

	 public void createSceneGraph(SimpleUniverse su)
	  {

	//*** A cube to be included in the scene including its transformation group.

	    //The colours for the Appearance.
	  
	    
	   // URL imgURL = Thread.currentThread().getContextClassLoader().getResource("de/deliancourt/jarvis/media/Alphamaps/test.png");
	   /* Texture img = new TextureLoader(imgURL,null).getTexture();
	    ImageComponent2D myImage = (ImageComponent2D) img.getImage(0);
        Texture2D texture = new Texture2D();
        texture = new Texture2D(Texture2D.BASE_LEVEL, Texture2D.RGBA,512,512);
        texture.setImage(0, myImage);
        texture.setEnable(true);
          fBoxApp.setTexture(texture);
       */
       
	   // Tile testw = new Tile(new Point2d(100, 100), 100,100);

	 
	    BranchGroup theScene = new BranchGroup();
	    
	    //theScene.addChild(Boxy);
	    //theScene.addChild(tgBSphere);
	    //theScene.addChild(tgBSphere);
	    //theScene.addChild(testw);


	    //The bounding region for the background.
	   /* BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),Double.MAX_VALUE);
	    //Load the background image.
	     imgURL = Thread.currentThread().getContextClassLoader().getResource("de/deliancourt/jarvis/media/textures/background.jpg");
	    TextureLoader textureLoad = new TextureLoader(imgURL,null);
	    //Define the image as the background and add it to the scene.
	    Background bgImage = new Background(textureLoad.getImage());
	    bgImage.setApplicationBounds(bounds);
	    */
	    
	    theScene.compile();
	    su.addBranchGraph(theScene);


	    
	    //Add the scene to the universe.
	    

	  }






	  //Some light is added to the scene here.
	  public void addLight(SimpleUniverse su)
	  {

		
	    BranchGroup bgLight = new BranchGroup();

	    //Directional light.
	    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), Double.MAX_VALUE);
	    Color3f lightColour1 = new Color3f(1.0f,1.0f,1.0f);
	    Vector3f lightDir1  = new Vector3f(-1.0f,0.0f,-0.1f);
	    DirectionalLight light1 = new DirectionalLight(lightColour1, lightDir1);
	    light1.setInfluencingBounds(bounds);

	    bgLight.addChild(light1);


	    //Ambient light.
	    Color3f ambientLightColour = new Color3f(0.5f, 0.5f, 0.5f);
	    AmbientLight ambLight = new AmbientLight(ambientLightColour);
	    ambLight.setInfluencingBounds(bounds);
	    bgLight.addChild(ambLight);



	    su.addBranchGraph(bgLight);

	  }

}
